﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kaiser.Modules.Hiner.Repositories;
using Kaiser.Modules.Hiner.Models.Product;
using Kaiser.Modules.Hiner.Models.Product.SearchModels;
using Kaiser.Models;
using Kaiser.Modules.Hiner.Models;
using System.Data;

namespace Kaiser.Repositories.iBatis.HINER
{
    public class ProductRepositorie : IProductRepositorie
    {
        private IBatisHepler _batisHepler = null;
        public ProductRepositorie()
        {
            _batisHepler = new BatisHepler("SQLMap.Hinner.config");
        }

        public DataGrid<ProductBase> SearchProduct(ProductSearch search, PagingInfo pageinfo)
        {
            var result = new DataGrid<ProductBase>();

            int totalCount = 0;

            result.TableResult = _batisHepler.ExcuteSelect("SearchProduct", search, pageinfo.Size, pageinfo.Index + 1, out totalCount);

            result.total = totalCount;

            return result;
        }

        public DataTable GetProductList(ProductSearch search, PagingInfo pageinfo)
        {
            return _batisHepler.ExcuteSelect("SearchProduct", search);
        }

        public void Insert(Modules.Hiner.Models.Product.ProductBase model)
        {
            var result = _batisHepler.ExcuteInsert("InsertProduct", model);
        }

        public void Update(Modules.Hiner.Models.Product.ProductBase model)
        {
            var result = _batisHepler.ExcuteUpdate("UpdateProduct", model);
        }

        public int Delete(object primaryId)
        {
            var parms = new Hashtable();
            parms["ids"] = primaryId.ToString().Split(',');
            var result = _batisHepler.ExcuteDelete("DeleteProduct", primaryId);
            return result;
        }

        public Modules.Hiner.Models.Product.ProductBase Get(object primaryId)
        {

            ProductBase result = _batisHepler.ExcuteSelectObj("GetProduct", primaryId) as ProductBase;
            return result;
        }

        public DataTable GetProductBase(string hdeCode)
        {
            Hashtable parms = new Hashtable();
            parms.Add("param0", hdeCode);
            DataTable result = _batisHepler.ExcuteSelect("GetProductBase", parms);
            return result;
        }
        #region 上传
        /// <summary>
        /// 上传查询
        /// </summary>
        /// <param name="search"></param>
        /// <param name="pageinfo"></param>
        /// <returns></returns>
        public DataGrid<Modules.Hiner.Models.Product.Attachment> GetAttachmentList(Modules.Hiner.Models.Product.SearchModels.AttachmentSearch search, PagingInfo pageinfo)
        {
            var result = new DataGrid<Modules.Hiner.Models.Product.Attachment>();

            int totalCount = 0;

            result.TableResult = _batisHepler.ExcuteSelect("SearchAttachment", search, pageinfo.Size, pageinfo.Index + 1, out totalCount);

            result.total = totalCount;

            return result;
        }

        /// <summary>
        /// 删除
        /// </summary>
        public void DeleteAttachment(string pid)
        {
            Hashtable parms = new Hashtable();
            parms["ids"] = pid.Split(',');
            var result = _batisHepler.ExcuteDelete("DeleteAttachment", parms);
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        public int InsertAttachment(Modules.Hiner.Models.Product.Attachment model)
        {
            var result = _batisHepler.ExcuteInsert("InsertAttachment", model);
            return Convert.ToInt32(result);
        }

        public Modules.Hiner.Models.Product.Attachment GetAttachment(string pid)
        {
            var result = _batisHepler.ExcuteSelectObj("GetAttachment", pid) as Modules.Hiner.Models.Product.Attachment;
            return result;
        }
        #endregion


        public IList<Photo> GetProductPhotoList(string objectID)
        {

            var result = _batisHepler.ExcuteSelectForList<Photo>("GetProductPhotoList", objectID);
            return result;
        }



        public bool VaidHinerCode(string code, string id)
        {
            ProductSearch search = new ProductSearch();
            search.ProductId = id;
            search.ProductHinerCode = code;
            var result = _batisHepler.ExcuteSelect("GetProductList", search);
            return result.Rows.Count > 0;
        }

        public bool ValidHDECode(string code, string id)
        {
            ProductSearch search = new ProductSearch();
            search.ProductId = id;
            search.ProductHDECode = code;
            var result = _batisHepler.ExcuteSelect("GetProductList", search);
            return result.Rows.Count > 0;
        }


        public int DeleteList(IList<string> ids)
        {
            throw new NotImplementedException();
        }
    }
}
